<template>
  <div class="bottom-menu">
    <check-button
      class="all-selector"
      @checkButton="checkButton"
      v-model="isSelectAll"
    ></check-button>
    <span>全选</span>
    <span class="total-price">合计：${{ totolPrice }}</span>
    <span class="buy">去结算({{ buyLength }})</span>
  </div>
</template>

<script>
import CheckButton from "./CheckButton.vue";

import { mapGetters } from "vuex";
export default {
  components: { CheckButton },
  name: "CartBottomBar",
  computed: {
    ...mapGetters(["cartList", "cartLength"]),

    totolPrice() {
      return this.cartList
        .filter((item) => {
          return item.checked;
        })
        .reduce((preValue, item) => {
          return preValue + item.count * item.price;
        }, 0)
        .toFixed(2);
    },
    buyLength() {
      return this.cartList.filter((item) => {
        return item.checked;
      }).length;
    },
    isSelectAll() {
      // return (
      //   this.cartList.length !== 0 &&
      //   this.cartList.every((item) => item.checked)
      // );
      if (this.buyLength === 0) return false;
      for (let item of this.cartList) {
        if (!item.checked) {
          return false;
        }
      }
    },
  },
  methods: {
    checkButton() {
      // 1.判断是否有未选中的按钮
      let isSelectAllBtn = this.$store.getters.cartList.find(
        (item) => !item.checked
      );

      // 2.有未选中的内容, 则全部选中
      if (isSelectAllBtn) {
        this.$store.state.cartList.forEach((item) => {
          item.checked = true;
        });
      } else {
        this.$store.state.cartList.forEach((item) => {
          item.checked = false;
        });
      }
    },
  },
};
</script>

<style scoped>
.bottom-menu {
  position: fixed;
  bottom: 48px;
  left: 0;
  width: 100%;
  height: 44px;
  line-height: 44px;
  font-size: 14px;
  padding-left: 35px;
  background-color: rgb(230, 230, 230);
  box-shadow: 0 -2px 5px rgba(0, 0, 0, 0.2);
}

.bottom-menu .all-selector {
  position: absolute;
  line-height: 0;
  left: 12px;
  top: 12px;
  border-radius: 50%;
  background-color: white;
}

.bottom-menu .total-price {
  margin-left: 30px;
  font-size: 15px;
  color: #666;
}

.bottom-menu .buy {
  background-color: orangered;
  color: white;
  width: 100px;
  height: 44px;
  text-align: center;
  float: right;
}
</style>